home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
jwpsrc.zip
/
REFORMAT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
19KB
|
731 lines
/* Copyright (C) Stephen Chung, 1991-1993. All rights reserved. */
#include "jwp.h"
BOOL NullParaFormatProc(FILEOPTIONS *f, PARAGRAPH far *pp, int n);
static UNIT far *CurrentPosition, far *TopPosition;
static POSITION StopPos;
static long int OldBottom;
static BOOL TopChanged;
static BOOL (* NewParaFormatProc)(FILEOPTIONS *, PARAGRAPH far *, int) = NullParaFormatProc;
static int nr_para = 0;
static BOOL NullParaFormatProc(FILEOPTIONS *f, PARAGRAPH far *pp, int n)
{
return (TRUE);
}
void SetReformatProc (BOOL (*f_pf)(FILEOPTIONS *, PARAGRAPH far *, int))
{
if (f_pf == NULL) NewParaFormatProc = NullParaFormatProc;
else NewParaFormatProc = f_pf;
}
static void SplitLine (FILEOPTIONS *f, POSITION p, int Options)
{
int i, j, len;
unsigned int n;
PARAGRAPH far *pp;
ONELINE far *lp;
/* How many blocks needed? */
len = unitlen(&UNITOF(p, POSOF(p)+1)) + 1;
n = len / TEXTBLOCKSIZE;
n = (n + 1) * TEXTBLOCKSIZE;
pp = StructAlloc(PARAGRAPH);
pp->leftindent = PARAOF(p)->leftindent;
pp->rightindent = PARAOF(p)->rightindent;
pp->firstindent = PARAOF(p)->firstindent;
pp->spacing = PARAOF(p)->spacing;
pp->spacemulti = PARAOF(p)->spacemulti;
pp->prev = PARAOF(p);
pp->next = PARAOF(p)->next;
PARAOF(p)->next = pp;
if (pp->next != NULL) pp->next->prev = pp;
else f->eof = pp;
pp->textsize = n;
lp = pp->lines = pp->lastline = StructAlloc(ONELINE);
f->nr_lines++;
lp->prev = lp->next = NULL;
lp->position = 0;
pp->text = (UNIT far *) BlockAlloc(n * sizeof(UNIT));
lp->length = len;
lp->height = lp->width = 0;
/* Copy the text */
for (i = 0, j = POSOF(p) + 1; ; i++, j++) {
if (Options & OP_MOVESEL) {
if (SELPARA1(f) == PARAOF(p) && &UNITOF(p, j) == &SELCHAR1(f)) {
SELPARA1(f) = pp;
SELPOS1(f) = i;
}
if (SELPARA2(f) == PARAOF(p) && &UNITOF(p, j) == &SELCHAR2(f)) {
SELPARA2(f) = pp;
SELPOS2(f) = i;
}
}
if (&UNITOF(f->current,CURCHAR(f)) == &UNITOF(p, j)) {
CURPARA(f) = pp;
CURLINE(f) = lp;
CURCHAR(f) = i;
}
if (CurrentPosition == &UNITOF(p, j)) CurrentPosition = &(pp->text[i]);
if (TopPosition == &UNITOF(p, j)) {
TopPosition = &(pp->text[i]);
TopChanged = (j != 0);
}
pp->text[i] = UNITOF(p,j);
if (!CHAROF(p, j)) break;
}
PARAOF(p)->lastline = LINEOF(p);
LINEOF(p)->length = POSOF(p);
CHAROF(p, POSOF(p)) = 0;
f->nr_bytes--;
ReallocateText(p, unitlen(PARAOF(p)->text));
/* Special format? */
NewParaFormatProc(f, pp, nr_para + 1);
}
static void JoinLine (FILEOPTIONS *f, POSITION p, int Options)
{
int i, j;
PARAGRAPH far *pp;
ONELINE far *tp1, far *tp2;
/* Join with the next paragraph */
pp = PARAOF(p)->next;
if (pp->next == NULL && pp == f->eof) f->eof = PARAOF(p);
/* Do we need to shift the next paragraph's margins up? */
if (POSOF(p) <= 0) {
PARAOF(p)->firstindent = pp->firstindent;
PARAOF(p)->leftindent = pp->leftindent;
PARAOF(p)->rightindent = pp->rightindent;
}
/* How many blocks needed? */
ReallocateText (p, unitlen(pp->text) + unitlen(PARAOF(p)->text) + 1);
CurrentPosition = &UNITOF(f->current,CURCHAR(f));
TopPosition = TOPPARA(f)->text + TOPLINE(f)->position;
/* Is the cursor on the remaining text? */
for (i = POSOF(p) + 1; ; i++) {
if (CurrentPosition == &UNITOF(p,i)) {
CurrentPosition = &UNITOF(p,POSOF(p));
break;
} else if (!CHAROF(p,i)) break;
}
/* Copy the text */
for (i = 0, j = POSOF(p); ; i++, j++) {
if (Options & OP_MOVESEL) {
if (SELPARA1(f) == pp && &(pp->text[i]) == &SELCHAR1(f)) {
SELPARA1(f) = PARAOF(p);
SELPOS1(f) = LINEOF(p)->position + j;
}
if (SELPARA2(f) == pp && &(pp->text[i]) == &SELCHAR2(f)) {
SELPARA2(f) = PARAOF(p);
SELPOS2(f) = LINEOF(p)->position + j;
}
}
if (CurrentPosition == pp->text + i) CurrentPosition = &UNITOF(p, j);
if (TopPosition == pp->text + i) {
TopPosition = &UNITOF(p, j);
TopChanged = (j != 0);
}
UNITOF(p,j) = pp->text[i];
if (!CHAROF(p, j)) break;
}
PARAOF(p)->lastline = LINEOF(p);
LINEOF(p)->length = j;
/* Re-link */
PARAOF(p)->next = pp->next;
if (pp->next != NULL) pp->next->prev = PARAOF(p);
PARAOF(StopPos) = pp->next;
LINEOF(StopPos) = (pp->next != NULL) ? pp->next->lines : NULL;
POSOF(StopPos) = 0;
/* Gets rid of the next paragraph */
for (tp1 = pp->lines; tp1 != NULL; ) {
tp2 = tp1;
tp1 = tp1->next;
OldBottom += tp2->height + pp->spacing;
FreeStruct(tp2);
f->nr_lines--;
}
if (pp->text != NULL) FreeBlock(pp->text);
FreeStruct(pp);
f->nr_bytes--;
}
static BOOL IsWhiteSpace (KANJI ch)
{
if (ch <= ' ') return (TRUE);
return (FALSE);
}
static BOOL IsWordWrap (POSITION *p)
{
int i;
KANJI ch;
UNIT far *cp = &UNITOF(*p, POSOF(*p));
ch = cp->kanji;
if (ISKANJI(ch)) return (FALSE);
if (IsWhiteSpace(ch)) return (FALSE);
/* Back-seek */
for (i = POSOF(*p); i >= 0; i--, cp--) {
ch = cp->kanji;
if (ISKANJI(ch) || IsWhiteSpace(ch) || ch == '-') break;
}
if (i < 0) return (FALSE); /* Word longer than one line! */
POSOF(*p) = i + 1;
return (TRUE);
}
static BOOL PotentialWordWrap (POSITION p)
{
int i;
KANJI ch;
UNIT far *cp = &UNITOF(p, POSOF(p));
if (ISKANJI(cp->kanji)) {
if (POSOF(p) == 0) return (TRUE);
return (FALSE);
}
/* Skip white spaces */
for (i = POSOF(p); i > 0; i--, POSOF(p)--, cp--) {
ch = cp->kanji;
if (ISKANJI(ch)) return (FALSE);
if (!IsWhiteSpace(ch)) break;
}
if (i < 0) return (FALSE);
/* Now, is this word alone? */
return (!IsWordWrap(&p));
}
static BOOL OpenNewLine (FILEOPTIONS *f, POSITION p, int width, int height, int length)
{
UNIT far *cp = &UNITOF(p,POSOF(p));
BOOL CanStop = FALSE;
if (LINEOF(p)->next == NULL) {
LINEOF(p)->next = StructAlloc(ONELINE);
LINEOF(p)->next->prev = LINEOF(p);
LINEOF(p)->next->next = NULL;
LINEOF(p)->next->position = 0;
f->nr_lines++;
} else {
if (POS2ABS(p) == LINEOF(p)->next->position) CanStop = TRUE;
}
LINEOF(p)->height = height;
LINEOF(p)->width = width;
LINEOF(p)->length = length;
LINEOF(p) = LINEOF(p)->next;
LINEOF(p)->position = cp - PARAOF(p)->text;
if (!CanStop) LINEOF(p)->length = unitlen(cp);
return (CanStop);
}
void ReformatParagraph (FILEOPTIONS *f, POSITION start, PARAGRAPH far *stop, int options)
{
int i, j, r, w;
long int OldTop, NewBottom;
int BlockHeight = LINEGAP(f);
int MaxHeight, LineLength;
long int dimension;
UNIT far *cp;
POSITION p;
RECT rect;
BOOL GoneBack = FALSE, CanStop;
BOOL relaxed, SoftReturn;
HDC hdc, hdcmem;
HBRUSH hbrush;
HBITMAP hbitmap;
TopChanged = FALSE;
/* Word wrap situation? */
if (PotentialWordWrap(start) && LINEOF(start)->prev != NULL) {
p = start;
LINEOF(start) = LINEOF(start)->prev;
POSOF(start) = LINEOF(start)->length - 1;
GoneBack = TRUE;
}
/* Now take care of some accounting */
CurrentPosition = &UNITOF(f->current,CURCHAR(f));
TopPosition = &(TOPPARA(f)->text[TOPLINE(f)->position]);
if (stop != NULL) {
PARAOF(StopPos) = stop;
LINEOF(StopPos) = stop->lines;
POSOF(StopPos) = 0;